package com.sky.controller;


import cn.hutool.core.util.PhoneUtil;
import com.aliyuncs.exceptions.ClientException;
import com.sky.result.Result;
import com.sky.utils.RandomCodeUtils;
import com.sky.utils.SmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;


/**
 * @BelongsProject: scrm
 * @BelongsPackage: com.puren.scrm.bussiness.controller.system
 * @Author: ouyangdongfang
 * @date: 2023-01-04  08:06
 */
@RestController
@Api( tags = "短信接口")
@RequestMapping("/sms")
@Slf4j
public class SmsController {
//    @Autowired
//    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    @ApiOperation(value = "短信通知接口", notes = "短信通知接口")
    public Result<String> sendMsg(@RequestParam("phoneNumber") String phoneNumber) throws Exception {
        log.info("发送短信，手机号：{}", phoneNumber);
        if(!PhoneUtil.isMobile(phoneNumber)){
            return Result.error("手机号格式不正");
        }
//        if (!Pattern.matches("^1[3-9]\\d{9}$", phoneNumber)) {
//            return Result.error("请输入正确的手机号码！");
//        }
        String vCode = new  RandomCodeUtils().createRandomCode(6);
        log.info("发送的验证码：{}", vCode);

        Result<String> Result = SmsUtil.sendSms(phoneNumber, vCode);
        if(Result.getCode()==1){
            redisTemplate.opsForValue().set(phoneNumber,vCode,3, TimeUnit.MINUTES);
        }
        return Result;
    }

}
